package com.jz.controller;

import com.jz.Order;
import com.jz.Product;
import com.jz.feign.ProductFeign;
import com.jz.service.OrderService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;

//@RestController
//@RequestMapping("order")
@Slf4j
public class OrderController1 {

    @Autowired
    private OrderService orderService;

    @Autowired
    private RestTemplate restTemplate;



    @GetMapping("createOrder/{pid}")
    @HystrixCommand(fallbackMethod = "fallbackMethod")
    public String createOrder(@PathVariable("pid")Integer pid){
        Order order=new Order();
        order.setUid(1);
        order.setUsername("张三");
        order.setNumber(1);
        Product product = restTemplate.getForObject("http://shop-product-hh/product/findById/" + pid, Product.class);
        order.setPprice(product.getPprice());
        order.setPname(product.getPname());
        order.setPid(product.getPid());
        orderService.createOrder(order);
        return "下单成功";
    }

    public String fallbackMethod(Integer pid){
        return "商品id号为"+pid+"无法访问！";
    }


    @GetMapping("getOrder")
    public String getOrder(){
        return "这里是测试";
    }
}
